package com.young.api;

import com.github.pagehelper.PageInfo;

import com.young.annot.Log;
import com.young.domain.SysRole;
import com.young.domain.SysUser;
import com.young.req.AddRoleReq;
import com.young.req.SysRolePageReq;
import com.young.req.UpdateRoleReq;
import com.young.res.RoleInfoRes;
import com.young.shop.service.SysRoleService;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 角色相关api
 * @Author: kangyang
 * @Date: 2021/5/15 17:58
 **/
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

  private static final Logger log = LoggerFactory.getLogger(SysRoleController.class);

  @Autowired
  private SysRoleService sysRoleService;


  @GetMapping("/list")
  public ResponseEntity<List<SysRole>> getRoleList() {
    log.info("请求获取角色列表接口开始");
    List<SysRole> sysRoles = sysRoleService.getRoleList();
    log.info("请求获取角色列表接口结束，响应参数{}", sysRoles);
    return ResponseEntity.ok(sysRoles);
  }

  @GetMapping("/page")
  public ResponseEntity<PageInfo<SysRole>> getRolePage(SysRolePageReq sysRolePageReq) {
    log.info("分页查询角色列表接口开始， 请求参数{}", sysRolePageReq);
    PageInfo<SysRole> sysRolesPage = sysRoleService.getRolePage(sysRolePageReq);
    log.info("分页查询角色列表接口结束，响应参数{}", sysRolesPage);
    return ResponseEntity.ok(sysRolesPage);
  }

  @PostMapping
  public ResponseEntity<Boolean> addRole(@RequestBody AddRoleReq addRoleReq) {
    log.info("新增角色开始， 请求参数{}", addRoleReq);
    SysUser principal = (SysUser) SecurityUtils.getSubject().getPrincipal();
    addRoleReq.setCreateUserId(principal.getUserId());
    Boolean result = sysRoleService.insertRole(addRoleReq);
    log.info("新增角色结束，响应参数{}", result);
    return ResponseEntity.ok(result);
  }

  @GetMapping("/info/{roleId}")
  @ApiOperation("根据角色Id查询角色信息")
  @Log(name = "根据角色Id查询角色信息")
  public ResponseEntity<RoleInfoRes> getCurrentRoleInfo(
      @PathVariable(value = "roleId", required = true) Long roleId) {
    log.info("根据角色Id查询角色信息开始， 请求参数{}", roleId);
    RoleInfoRes roleInfoRes = sysRoleService.getCurrentRoleByRoleId(roleId);
    log.info("根据角色Id查询角色信息结束，响应参数{}", roleInfoRes);
    return ResponseEntity.ok(roleInfoRes);
  }

  @DeleteMapping
  @ApiOperation("删除角色")
  @Log(name = "删除角色")
  public ResponseEntity<Boolean> deleteRoleByRoleIds(@RequestBody List<Long> roleIds) {
    log.info("删除角色开始， 请求参数{}", roleIds);
    Boolean result = sysRoleService.deleteRoleByRoleIds(roleIds);
    log.info("删除角色结束，响应参数{}", result);
    return ResponseEntity.ok(result);
  }

  @PutMapping
  @ApiOperation("编辑角色信息")
  @Log(name = "编辑角色信息")
  public ResponseEntity<Boolean> updateRole(@RequestBody UpdateRoleReq updateRoleReq) {
    log.info("编辑角色信息开始， 请求参数{}", updateRoleReq);
    Boolean result = sysRoleService.updateRoleInfo(updateRoleReq);
    log.info("编辑角色信息结束，响应参数{}", result);
    return ResponseEntity.ok(result);
  }
}
